#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
using namespace std;

class Solution {
public:
    bool visited[21][21] = { 0 };
    int zero = 0;
    int ans = 0;
    void dfs(vector<vector<int>>& grid, int i, int j, int num)
    {
        if (num == zero && grid[i][j] == 2)
        {
            ++ans;
            return;
        }
        int dics[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
        for (auto dic : dics)
        {
            int x = i + dic[0], y = j + dic[1];
            if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size() && !visited[x][y] && grid[x][y] != -1)
            {
                visited[x][y] = true;
                dfs(grid, x, y, num + 1);
                visited[x][y] = false;
            }
        }
    }

    int uniquePathsIII(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size();
        int bx, by;
        for (int i = 0; i < m; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                if (grid[i][j] == 0) ++zero;
                else if (grid[i][j] == 1)
                {
                    bx = i, by = j;
                }
            }
        }
        zero += 2;
        visited[bx][by] = true;
        dfs(grid, bx, by, 1);
        visited[bx][by] = false;
        return ans;
    }

    int LPF(int n)
    {
        for (int i = 2; i <= n / 2; ++i)
        {
            if (n % i == 0) return i;
        }
        return n;
    }

    int minOperations(vector<int>& nums) {
        int ans = 0;
        for (int i = nums.size() - 2; i >= 0; --i)
        {
            if (nums[i] > nums[i + 1])
            {
                nums[i] = LPF(nums[i]);
                if (nums[i] > nums[i + 1]) return -1;
                ++ans;
            }
        }
        return ans;
    }
};